<template>
	<view>
		<scroll-view scroll-y class="page">
			<!-- 头部 -->
			<cu-custom bgColor="bg-rgb" :isBack="true">
				<block slot="backText">返回</block>
				<block slot="content">制令单发料项目</block>
				<view slot="right" @tap="rightClick">凭证抬头</view>
			</cu-custom>
			<!-- 查询条件 -->
			<view class="fixed" :style="[{animationDelay: 0.1 + 's'}]">
				<form>
					<view class="cu-form-group last">
						<image class="icon" src="../../static/saomiao.png" mode=""></image>
						<input auto-focus placeholder="请扫描二维码搜索" name="input" v-model="myFormData.materielGauge"
							@confirm="inputQuery"></input>
					</view>
				</form>
			</view>
			<!-- 抬头凭证 输入弹窗 -->
			<template v-if="modalName=='Modal'">
				<view class="cu-modal" :class="modalName=='Modal'?'show':''">
					<view class="cu-dialog">
						<view class="cu-bar bg-white justify-end">
							<view class="content">凭证抬头</view>
							<view class="action">
								<text class="cuIcon-close text-red" @click="close"></text>
							</view>
						</view>
						<view class="padding-xl">
							<form>
								<view class="cu-form-group">
									<view class="titles">凭证日期：</view>
									<picker mode="date" :value="myModelData.cerDate" @change="cerDateChange">
										<view class="picker">
											{{myModelData.cerDate}}
										</view>
									</picker>
								</view>
								<view class="cu-form-group">
									<view class="titles">过账日期：</view>
									<picker mode="date" :value="myModelData.accountDate" @change="accountDateChange">
										<view class="picker">
											{{myModelData.accountDate}}
										</view>
									</picker>
								</view>
								<view class="cu-form-group">
									<view class="titls">类型：</view>
									<input placeholder="工号" style="text-align: right;" name="input"
										v-model="myModelData.affairType" disabled></input>
								</view>
								<view class="cu-form-group">
									<view class="titles">描述：</view>
									<textarea value="" placeholder="请输入描述" style="text-align: left;text-indent: 2em;"
										v-model="myModelData.affairContent" />
								</view>
								<view class="padding flex flex-direction">
									<button class="cu-btn lg" @click="onSubmit">确定</button>
								</view>
							</form>
						</view>
					</view>
				</view>
			</template>
			<!-- 详情 -->
			<view class="list">
				<view class="cu-list menu animation-slide-bottom" v-for="(item,index) in WordListMsg" :key="index"
					:style="[{animationDelay: (index + 1)*0.1 + 's'}]">
					<view class="cu-item">
						<view class="content">
							<text class="text-grey headLine">料号</text>
						</view>
						<view class="action">
							<text class="text-grey headLine">{{item.materielCode}}</text>
						</view>
					</view>
					<view class="cu-item">
						<view class="content">
							<text class="text-grey">客户料号</text>
						</view>
						<view class="action">
							<text class="text-grey">{{item.clientCode}}</text>
						</view>
					</view>
					<view class="cu-item">
						<view class="content">
							<text class="text-grey">货架灯</text>
						</view>
						<view class="action">
							<custom-select :dat="{item:item,num:index}" @w_xl="changeColor"></custom-select>
						</view>
					</view>
					<view class="cu-item">
						<view class="content">
							<text class="text-grey">物料名称</text>
						</view>
						<view class="action">
							<text class="text-grey">{{item.materielName}}</text>
						</view>
					</view>
					<view class="cu-item">
						<view class="content">
							<text class="text-grey">物料规格</text>
						</view>
						<view class="action">
							<text class="text-grey">{{item.materielGauge}}</text>
						</view>
					</view>
					<view class="cu-item">
						<view class="content">
							<text class="text-grey">提货数量</text>
						</view>
						<view class="action">
							<text class="text-grey">{{item.pickupNum}}</text>
						</view>
					</view>
					<view class="cu-item">
						<view class="content">
							<text class="text-grey">需求数量</text>
						</view>
						<view class="action">
							<text class="text-grey">{{item.realNum}}</text>
						</view>
					</view>
					<view class="cu-item">
						<view class="content">
							<text class="text-grey">已发料数量</text>
						</view>
						<view class="action">
							<text class="text-grey">{{item.deliveryNum}}</text>
						</view>
					</view>
					<view class="cu-item">
						<view class="content">
							<text class="text-grey">单位</text>
						</view>
						<view class="action">
							<text class="text-grey">{{item.countUnit}}</text>
						</view>
					</view>
					<view class="cu-item">
						<view class="numName" style="background-color: #99CC00;">
							发料数量:
						</view>
						<view class="numInput">
							<input style="width: 260rpx;" type="text" v-model="preservationList[index].num" value=""
								placeholder="请输入发料数量" />
						</view>
					</view>
					<view class="cu-item">
						<view class="btn1" @click="FirstArticleIssue(index,item)">发料</view>
						<view class="btn2" @click="scanShipment(index,item)">扫描发料</view>
					</view>
					<view class="line">

					</view>
				</view>
			</view>
			<view class="none" v-if="noDataAvailable">
				<view class="none1">
					暂无数据
				</view>
			</view>
			<view class="none2" v-if="noMoreDataAvailable">
				没有更多数据
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import customSelect from '@/components/select.vue'
	export default {
		name: 'sendmaterielDetail',
		data() {
			return {
				noDataAvailable: false,
				noMoreDataAvailable: false,
				produceId: '', // 制令单中的 生产订单ID
				produceGrade: '', // 制令单中的  生产阶别
				plantNum: '', // 制令单中  计划数量
				WordListMsg: [],
				preservationList: [],
				modalName: '',
				myModelData: {
					cerDate: '', //凭证日期
					accountDate: '', // 过账日期
					affairType: '',
					affairContent: ''
				},
				// 有一个子订单  次数数组就有几个对象 用于存储id + 发货数量
				preservationList: [],
				myFormData: {
					materielGauge: ''
				},
				pageNo:1
			}
		},
		components: {
			customSelect
		},
		onShow() {
			let data = {
				produceId: this.produceId,
				produceGrade: this.produceGrade,
				// ifDelivery: '未发料',
				planNum: this.plantNum,
				commandbillId: this.id,
				materielGauge: '',
				wholid:'',
				pageSize:5,
				pageNo:this.pageNo
			}
			this.WordListMsg = []
			this.listBom(data);
		},
		onLoad(e) {
			this.produceId = uni.getStorageSync('SsuingOfInstructionSheetProduceId');
			this.id = uni.getStorageSync('SsuingOfInstructionSheetId');
			this.produceGrade = uni.getStorageSync('SsuingOfInstructionSheetProduceGrade');
			this.plantNum = uni.getStorageSync('SsuingOfInstructionSheetPlantNum');

			let date = new Date()
			this.myModelData.cerDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
			this.myModelData.accountDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
			this.myModelData.affairType = uni.getStorageSync('affairType');
		},
		onReachBottom: function() {
			let _this = this
			if (this.noDataAvailable == true || this.noMoreDataAvailable == true) {
				return
			}
			_this.pageNo++
			let data = {
				produceId: _this.produceId,
				produceGrade: _this.produceGrade,
				// ifDelivery: '未发料',
				planNum: _this.plantNum,
				commandbillId: _this.id,
				materielGauge: '',
				wholid:_this.myFormData.materielGauge,
				pageSize:5,
				pageNo:_this.pageNo
			}
			_this.listBom(data)
		},
		methods: {
			// 选择灯
			changeColor(index, value) {
				//
				console.log(index, value);
				// this.WordListMsg[index].color = value
				let data = {
					color: value,
					mCode: this.WordListMsg[index].materielCode
				}
				this.$http.get('storage/mesStorageWholesale/sendLitLight', {
					params: data
				}).then(res => {
					uni.hideLoading()
					if (res.data.success) {
						// this.$tip.success(res.data.message)
						this.modal.show({
							type: 'success',
							content: res.data.message
						})
					} else {
						// this.$tip.error(res.data.message)
						this.modal.show({
							type: 'error',
							content: res.data.message
						})
					}
				})
			},
			// 制造中心-获取bom-分页列表查询
			getProduceMesCommandbillInfoListbom() {
				uni.showLoading({
					mask: true,
					title: '加载中.....'
				})
				this.$http.get('/storage/mesStorageWholesale/queryById', {
					params: {
						id: this.myFormData.materielGauge
					}
				}).then(res => {
					if (res.data.success) {
						if(res.data.result){
							let data = {
								produceId: this.produceId,
								produceGrade: this.produceGrade,
								// ifDelivery: '未发料',
								planNum: this.plantNum,
								id: this.id,
								materielGauge: res.data.result.productCode
							}
							this.listBom(data)
						}else{
							let data = {
								produceId: this.produceId,
								produceGrade: this.produceGrade,
								// ifDelivery: '未发料',
								planNum: this.plantNum,
								id: this.id,
								materielGauge: this.myFormData.materielGauge,
							}
							this.listBom(data)
						}
					} else {
						this.modal.show({
							type: 'error',
							content: res.data.message
						})
					}
				})

			},
			listBom(data){
				this.noDataAvailable = false
				this.noMoreDataAvailable = false
				this.$http.get('produce/mesCommandbillPitem/list', {
					params: data
				}).then(res => {
					uni.hideLoading()
					this.myFormData.materielGauge = ''
					if (res.data.success) {
						this.WordListMsg = this.WordListMsg.concat(res.data.result.records);
						if (res.data.result.total == 0) {
							this.noDataAvailable = true;
						} else if(this.WordListMsg.length >= res.data.result.total){
							this.noMoreDataAvailable = true;
						}
						this.preservationList = []
						for (let i = 0; i < this.WordListMsg.length; i++) {
							let data = {
								id: this.WordListMsg[i].id,
								num: ''
							}
							this.preservationList.push(data)
						}
						console.log(this.WordListMsg)
					} else {
						// this.$tip.error(res.data.message)
						this.modal.show({
							type: 'error',
							content: res.data.message
						})
					}
				})
			},
			// 输入框查询
			inputQuery() {
				this.pageNo = 1
				let data = {
					produceId: this.produceId,
					produceGrade: this.produceGrade,
					// ifDelivery: '未发料',
					planNum: this.plantNum,
					commandbillId: this.id,
					materielGauge: '',
					wholid:this.myFormData.materielGauge,
					pageSize:5,
					pageNo:this.pageNo
				}
				this.WordListMsg = []
				this.preservationList = []
				this.listBom(data)
				console.log(this.myFormData.materielCode)
				// this.getProduceMesCommandbillInfoListbom();

			},
			// 凭证抬头 按钮
			rightClick() {
				this.modalName = 'Modal'
			},
			// 关闭弹窗
			close() {
				this.modalName = '';
				this.myModelData.affairContent = '';
			},
			// 凭证日期
			cerDateChange(e) {
				console.log(e)
				this.myModelData.cerDate = e.detail.value
			},
			accountDateChange(e) {
				this.myModelData.accountDate = e.detail.value
			},
			onSubmit() {
				uni.showLoading({
					mask: true,
					title: '添加凭证抬头中.....'
				})
				if (this.myModelData.affairContent.replace(/\s+/g, "") == '') {
					// this.$tip.error('描述不能为空');
					this.modal.show({
						type: 'error',
						content: '描述不能为空'
					})
					uni.hideLoading()
					return false
				}
				this.$http.post('storage/mesCertificatePerk/add', this.myModelData).then(res => {
					console.log(res)
					uni.hideLoading()
					if (res.data.success) {
						uni.setStorageSync('SsuingOfInstructionSheetDetailRise', res.data.result.id);
						// this.$tip.success(res.data.message)
						this.modal.show({
							type: 'success',
							content: res.data.message
						})
						this.modalName = '';
						this.myModelData.affairContent = '';
					} else {
						// this.$tip.error(res.data.message)
						this.modal.show({
							type: 'error',
							content: res.data.message
						})
					}
				})
			},
			// 发料
			FirstArticleIssue(index, item) {
				console.log(item)
				if (this.preservationList[index].num == '' || this.preservationList[index].num == 0) {
					// this.$tip.error('制令单发料数量不能为空')
					this.modal.show({
						type: 'error',
						content: '制令单发料数量不能为空'
					})
					return false
				} else if (!uni.getStorageSync('SsuingOfInstructionSheetDetailRise')) {
					let date = new Date()
					this.myModelData.cerDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
					this.myModelData.accountDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
					this.myModelData.affairType = uni.getStorageSync('affairType');
					this.modalName = 'Modal'
					return false
				}
				uni.showModal({
					title: '制令单发料',
					content: '确认要发料吗?',
					cancelText: '取消',
					confirmText: '确认',
					success: res => {
						if (res.confirm) {
							uni.showLoading({
								mask: true,
								title: '发料中.....'
							})
							let data = {
								mobileCode: 261,
								mobileType: '制令单发料',
								materielCode: item.materielCode,
								perkId: uni.getStorageSync('SsuingOfInstructionSheetDetailRise'),
								reserveCode: this.preservationList[index].id,
								inputNum: this.preservationList[index].num,
								query5: item.commandbillId
							}
							// this.$http.post('storage/mesCertificatePerk/addMesCertificateItem',data).then( res =>{
							this.$http.post('storage/mesCertificatePerk/addSendMateriel', data).then(res => {
								uni.hideLoading()
								console.log(res)
								if (res.data.success) {
									// this.$tip.success(res.data.message)
									this.modal.show({
										type: 'success',
										content: res.data.message
									})

									// this.getProduceMesCommandbillInfoListbom();
									this.inputQuery()
								} else {
									// this.$tip.error(res.data.message)
									this.modal.show({
										type: 'error',
										content: res.data.message
									})
								}
							})
						}
					}
				})
			},
			// 扫描发料
			scanShipment(index, item) {
				if (!uni.getStorageSync('SsuingOfInstructionSheetDetailRise')) {
					this.modalName = 'Modal'
					return false
				}
				uni.showModal({
					title: '制令单扫描发料',
					content: '确认要扫描发料吗?',
					cancelText: '取消',
					confirmText: '确认',
					success: res => {
						if (res.confirm) {
							uni.setStorageSync('materielCode', item.materielCode);
							uni.showLoading({
								mask: true,
								title: '扫描发料中.....'
							})
							let data = {
								mobileCode: 264,
								mobileType: '制令单扫描发料',
								perkId: uni.getStorageSync('SsuingOfInstructionSheetDetailRise'),
								reserveCode: this.preservationList[index].id,
								inputNum: this.preservationList[index].num,
								commandId: item.id,
								materielCode: item.materielCode

							}
							// this.$http.post('storage/mesCertificatePerk/addMesCertificateItem',data).then( res =>{
							this.$http.post('storage/mesCertificatePerk/scanSendMateriel', data).then(res => {
								console.log(res.data.result.id)
								uni.hideLoading()
								if (res.data.success) {
									uni.setStorageSync('SsuingOfInstructionSheetDetailId', res.data
										.result.id)
									uni.setStorageSync('SsuingOfInstructionSheetCommandId', res.data
										.result.commandId)
									uni.setStorageSync('SsuingOfInstructionRealNum', item.realNum)
									this.$Router.push({
										name: 'SsuingOfInstructionSheetDetailSaoMiao'
									})
								} else {
									// this.$tip.error(res.data.message)
									this.modal.show({
										type: 'error',
										content: res.data.message
									})
								}
							})
						}
					}
				})
			},
		},
	}
</script>

<style lang="less">
	page {
		background-color: #f1f1f1;

		.fixed {
			position: fixed;
			z-index: 1;
			width: 100%;
			background-color: white;
			height: 100rpx;
			border-bottom: 1rpx solid #eee;

			.last {
				border-bottom: 1rpx solid #eee;
				position: relative;

				.icon {
					width: 30rpx;
					height: 29rpx;
					position: absolute;
					top: 36rpx;
					left: 60rpx;
				}

				input {
					width: 686rpx;
					height: 68rpx;
					background: #FFFFFF;
					border: 1px solid #D5D8DF;
					border-radius: 34px;
					padding-left: 79rpx;
				}
			}
		}
	}

	.list {
		margin-top: 120rpx;

		.action {
			width: 70%;
			word-break: break-all;
			text-align: right;
		}
	}

	.title {
		font-weight: bold;
		padding: 10rpx;
		border-radius: 10rpx;
		color: white;
	}

	input {
		border: 2rpx solid #F0F0F0;
		height: 60rpx;
		padding: 10rpx;
		border-radius: 10rpx;
		font-size: 20rpx;
	}

	.btn1,
	.btn2 {
		width: 180rpx;
		height: 64rpx;
		line-height: 50rpx;
		color: white;
		border-radius: 30rpx;
		padding: 10rpx 20rpx;
		text-align: center;
	}

	.btn1 {
		background-image: radial-gradient(circle, #3F55E9, #5F71ED, #7D8CF0);
	}

	.btn2 {
		background-image: radial-gradient(circle, #716BA9, #362D85, #57509A);
	}

	.numName {
		font-weight: bold;
		padding: 10rpx;
		border-radius: 10rpx;
		color: white;
	}

	.headLine {
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #191919;
		line-height: 50rpx;
	}

	.cu-btn {
		background-image: radial-gradient(circle, #3F55E9, #5F71ED, #7D8CF0);
		color: white;
	}
	.none .none1 {
		font-size: 30rpx;
		text-align: center;
		margin-top: 50rpx;
		color: #999999;
	}

	.none2 {
		font-size: 30rpx;
		text-align: center;
		margin-top: 50rpx;
		color: #999999;
	}
</style>
